package cn.piaoyi.web.action;

import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.*;

import cn.piaoyi.fio.PicUploadFile;
import cn.piaoyi.fio.UploadFile;


public class AdminUploadPic extends BaseAction{

	private static final Log logger = LogFactory.getLog(AdminUploadPic.class);
	
	private PicUploadFile picUploadFile;

	public PicUploadFile getPicUploadFile() {
		return picUploadFile;
	}

	public void setPicUploadFile(PicUploadFile picUploadFile) {
		this.picUploadFile = picUploadFile;
	}	
	
	private String editId;
	private File file;
	private String fileFileName;
	private String fileContentType;

	public String getEditId() {
		return editId;
	}

	public void setEditId(String editId) {
		this.editId = editId;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}


	private String message = "";
	private String picPath;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getPicPath() {
		return picPath;
	}

	public void setPicPath(String picPath) {
		this.picPath = picPath;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}
	
	public String index(){
		this.setAction("upload");
		return SUCCESS;
	}

	public String upload() {
		UploadFile picFile = null;
		if (this.getFile() != null
				&& StringUtils.isNotBlank(this.getFileFileName())) {
			try {
				picFile = new UploadFile();
				picFile.setFileName(this.getFileFileName());
				picFile.setInputStream(new FileInputStream(this.getFile()));
				this.setPicPath(this.getPicUploadFile().saveUploadFile(picFile));
				this.message = "true";
			} catch (Exception ex) {
				picFile = null;
			}
		} else {
			this.message = "上传图片错误!请重新上传";
		}
		return SUCCESS;
	}
}
